#include "code_gen.h"

extern int offset_count; /* declared in register.c */

void generate_code_fix_sp(int n) {
	if(n > 6) {
		code("\t movq %%rbp, %%rsp\n");
		code("\t addq $%d, %%rsp\n", -8*(n-6-offset_count));
	} else {
		code("\t movq %%rbp, %%rsp\n");
		code("\t addq $%d, %%rsp\n", 8*(offset_count));
	}
}

int generate_code_mov_free_args(tree_node_t *node, int n) {
	if(n <= 6) {
		code("\t movq %d(%%rbp), %s\n", 8*(reg_get_ptr(node->reg_id)->offset), reg_get_string(n-1));
	}

	reg_free_var(node->reg_id);

	if(RIGHT_CHILD(node) != NULL && RIGHT_CHILD(node)->op_type == OP_ARG) {
		return generate_code_mov_free_args(RIGHT_CHILD(node), n+1);
	}

	return n;
}
